<template>
  <view class="container">
    <view class="nav-card" @click="navTo('record')">
      <image src="/static/icons/target.png" class="nav-icon"/>
      <text class="nav-text">开始训练</text>
    </view>
    
    <view class="nav-card" @click="navTo('history')">
      <image src="/static/icons/history.png" class="nav-icon"/>
      <text class="nav-text">历史记录</text>
    </view>
  </view>
</template>

<script>
export default {
  onLoad() {
    this.getUserId();
  },
  methods: {
    async getUserId() {
      try {
        // 获取用户OpenID
        const loginRes = await uni.login();
        if (loginRes.code) {
          // 这里需要调用你的后端接口，用code换取openid
          // 暂时使用一个临时方案：生成一个唯一ID并存储
          let userId = uni.getStorageSync('userId');
          if (!userId) {
            // 如果没有存储过用户ID，生成一个
            userId = 'user_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
            uni.setStorageSync('userId', userId);
          }
          
          // 将用户ID设为全局变量
          getApp().globalData.userId = userId;
        }
      } catch (error) {
        console.error('获取用户ID失败:', error);
        // 生成临时用户ID
        let userId = uni.getStorageSync('userId');
        if (!userId) {
          userId = 'temp_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
          uni.setStorageSync('userId', userId);
        }
        getApp().globalData.userId = userId;
      }
    },
    
    navTo(path) {
      uni.navigateTo({
        url: `/pages/${path}/${path}`
      })
    }
  }
}
</script>

<style>
.container {
  padding: 30rpx;
  display: grid;
  gap: 40rpx;
}
.nav-card {
  height: 300rpx;
  background: #fff;
  border-radius: 20rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.1);
}
.nav-icon {
  width: 120rpx;
  height: 120rpx;
  margin-bottom: 20rpx;
}
.nav-text {
  font-size: 32rpx;
  font-weight: 500;
  color: #333;
}
</style>